package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.ExcellentCourseDao;
import com.resourcestore.dao.MajorDao;
import com.resourcestore.model.ExcellentCourse;
import com.resourcestore.model.Major;
import com.resourcestore.utils.tag.Pagination;

public class ExcellentCourseAction extends BaseAction {

	private Major major;
	private MajorDao majorDao = new MajorDao();
	private ExcellentCourse excellentCourse;
	private List<ExcellentCourse> listExcellentCourse;
	private ExcellentCourseDao excellentCourseDao = new ExcellentCourseDao();
	private Pagination pagination = new Pagination();
	private List<Major> preInsertExcellentCourse;

	/**
	 * 查询返回List
	 * 
	 * @throws ParseException
	 * 
	 */
	public String listExcellentCourse() {
		excellentCourse = new ExcellentCourse();
		pagination.setTotal(excellentCourseDao
				.queryExcellentCourseNum(excellentCourse));
		listExcellentCourse = excellentCourseDao.selectExcellentCoursePage(
				excellentCourse, pagination);

		for (int i = 0; i < listExcellentCourse.size(); i++) {
			try {
				listExcellentCourse.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listExcellentCourse
								.get(i).getGmtCreated()));
				listExcellentCourse.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listExcellentCourse
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return "default";
	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListExcellentCourse() {

		// 对内容进行urldecoder解码
		try {
			excellentCourse.setExceCourseName(new String(excellentCourse
					.getExceCourseName().getBytes("iso-8859-1"), "UTF-8"));
			excellentCourse.setMajorName(new String(excellentCourse
					.getMajorName().getBytes("iso-8859-1"), "UTF-8"));
			excellentCourse.setDepartmentName(new String(excellentCourse
					.getDepartmentName().getBytes("iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(excellentCourseDao
				.queryExcellentCourseNum(excellentCourse));
		listExcellentCourse = excellentCourseDao.selectExcellentCoursePage(
				excellentCourse, pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listExcellentCourse.size(); i++) {
			try {
				listExcellentCourse.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listExcellentCourse
								.get(i).getGmtCreated()));
				listExcellentCourse.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listExcellentCourse
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/ExcellentCourse/listExcellentCourse.jsp");
	}

	/**
	 * 预处理插入信息
	 */

	public String preInsertExcellentCourse() {
//		preInsertExcellentCourse = majorDao.selectMajor();
//
//		for (int i = 0; i < preInsertExcellentCourse.size(); i++) {
//			try {
//				preInsertExcellentCourse
//						.get(i)
//						.setGmtCreatedString(
//								CommonUtil
//										.getSysTimeFormatString(preInsertExcellentCourse
//												.get(i).getGmtCreated()));
//				preInsertExcellentCourse
//						.get(i)
//						.setGmtModifiedString(
//								CommonUtil
//										.getSysTimeFormatString(preInsertExcellentCourse
//												.get(i).getGmtModified()));
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		return newpath("/ExcellentCourse/insertExcellentCourse.jsp");

	}

	/**
	 * 插入一条数据
	 */
	public String insertExcellentCourse() {

		try {
			excellentCourse.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			excellentCourse.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		excellentCourseDao.insertExcellentCourse(excellentCourse);
		return redirect(getUrl());
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateExcellentCourse() {

		excellentCourse = excellentCourseDao
				.selectExcellentCourseById(excellentCourse);

		//preInsertExcellentCourse = majorDao.selectMajor();

		return newpath("/ExcellentCourse/updateExcellentCourse.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateExcellentCourse() {

		try {
			// excellentCourse.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			excellentCourse.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		excellentCourseDao.updateExcellentCourse(excellentCourse);
		return redirect(getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteExcellentCourse() {
		excellentCourseDao.deleteExcellentCourse(excellentCourse);
		return redirect(getUrl());
	}

	public String deleteMoreExcellentCourse() {
		
		excellentCourseDao.deleteMoreExcellentCourse(excellentCourse);
		return redirect(getUrl());
	}

	public ExcellentCourse getExcellentCourse() {
		return excellentCourse;
	}

	public void setExcellentCourse(ExcellentCourse excellentCourse) {
		this.excellentCourse = excellentCourse;
	}

	public List<ExcellentCourse> getListExcellentCourse() {
		return listExcellentCourse;
	}

	public void setListExcellentCourse(List<ExcellentCourse> listExcellentCourse) {
		this.listExcellentCourse = listExcellentCourse;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public Major getMajor() {
		return major;
	}

	public void setMajor(Major major) {
		this.major = major;
	}

	public List<Major> getPreInsertExcellentCourse() {
		return preInsertExcellentCourse;
	}

	public void setPreInsertExcellentCourse(List<Major> preInsertExcellentCourse) {
		this.preInsertExcellentCourse = preInsertExcellentCourse;
	}

}
